{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f6cd6d0-1463-43ab-aca0-72f6dd184017",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "  有二进制版本的，但源代码版本是后来的:\n",
      "     binary source needs_compilation\n",
      "coin  1.4-2  1.4-3              TRUE\n",
      "\n",
      "  Binaries will be installed\n",
      "package 'coin' successfully unpacked and MD5 sums checked\n",
      "\n",
      "The downloaded binary packages are in\n",
      "\tC:\\Users\\kang\\AppData\\Local\\Temp\\RtmpopQnXW\\downloaded_packages\n"
     ]
    }
   ],
   "source": [
    "options(repos = c(CRAN = \"https://mirrors.aliyun.com/CRAN/\"))\n",
    "install.packages(\"coin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "76195f31-2574-4e67-8dc7-2aa55e92eab0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   treatment score\n",
      "1          A    40\n",
      "2          A    57\n",
      "3          A    45\n",
      "4          A    55\n",
      "5          A    58\n",
      "6          B    57\n",
      "7          B    64\n",
      "8          B    55\n",
      "9          B    62\n",
      "10         B    65\n"
     ]
    }
   ],
   "source": [
    "score <- c(40,57,45,55,58,57,64,55,62,65)\n",
    "treatment <- factor(c(rep(\"A\",5),rep(\"B\",5)))\n",
    "mydata <- data.frame(treatment,score)\n",
    "print(mydata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "434ae4c0-b938-414b-8226-fee7ef39437a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "\tTwo Sample t-test\n",
       "\n",
       "data:  score by treatment\n",
       "t = -2.345, df = 8, p-value = 0.04705\n",
       "alternative hypothesis: true difference in means between group A and group B is not equal to 0\n",
       "95 percent confidence interval:\n",
       " -19.0405455  -0.1594545\n",
       "sample estimates:\n",
       "mean in group A mean in group B \n",
       "           51.0            60.6 \n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "t.test(score~treatment,data=mydata,var.equal=TRUE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "10744df1-8034-4a59-8616-5d6ec338eb84",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning message:\n",
      "\"程辑包'coin'是用R版本4.1.3 来建造的\"\n",
      "载入需要的程辑包：survival\n",
      "\n",
      "Warning message:\n",
      "\"程辑包'survival'是用R版本4.1.3 来建造的\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\tExact Two-Sample Fisher-Pitman Permutation Test\n",
       "\n",
       "data:  score by treatment (A, B)\n",
       "Z = -1.9147, p-value = 0.07143\n",
       "alternative hypothesis: true mu is not equal to 0\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "library(coin)\n",
    "library(survival)\n",
    "oneway_test(score~treatment,data=mydata,distribution=\"exact\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1d6af3ac-234e-417b-bd1f-8e700fbe0595",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "\tWilcoxon rank sum exact test\n",
       "\n",
       "data:  Prob by So\n",
       "W = 81, p-value = 8.488e-05\n",
       "alternative hypothesis: true location shift is not equal to 0\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "library(MASS)\n",
    "\n",
    "UScrime <- transform(UScrime,So=factor(So))\n",
    "wilcox.test(Prob~So,data=UScrime,distribution=\"exact\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fbce4093-a231-4883-bc76-26819a594648",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  So       Prob\n",
      "1  0 0.03851265\n",
      "2  1 0.06371269\n"
     ]
    }
   ],
   "source": [
    "print(aggregate(Prob~So,data=UScrime,FUN=mean))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "99ec4c67-ef79-42aa-97f4-14d025da5373",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "载入需要的程辑包：mvtnorm\n",
      "\n",
      "Warning message:\n",
      "\"程辑包'mvtnorm'是用R版本4.1.1 来建造的\"\n",
      "载入需要的程辑包：TH.data\n",
      "\n",
      "\n",
      "载入程辑包：'TH.data'\n",
      "\n",
      "\n",
      "The following object is masked from 'package:MASS':\n",
      "\n",
      "    geyser\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "\tApproximative K-Sample Fisher-Pitman Permutation Test\n",
       "\n",
       "data:  response by\n",
       "\t trt (1time, 2times, 4times, drugD, drugE)\n",
       "chi-squared = 36.381, p-value < 1e-04\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "library(multcomp)\n",
    "set.seed(123)\n",
    "oneway_test(response~trt,data=cholesterol,distribution=approximate(nresample=10000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7edf3fbd-048b-4ad9-b3e4-cfdbb41a654e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7eee26e-bb55-4162-a27a-d029a599a5cf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "4.1.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
